<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2018/2/28
 * Time: 22:50
 */

use yii\helpers\Url;

$web = Url::base();
?>




<div class="form-horizontal" id="category_submit_form">
    <input type="hidden" id="id" name="id" class="form-control"
           value="<?= isset($item['id']) != null ? $item['id'] : '' ?>">
    <input type="hidden" id="parent_id" name="parent_id" class="form-control"
           value="<?= isset($item['parent_id']) ? $item['parent_id'] : '' ?>">

    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="name">父级分类：</label>
        <div class="col-lg-10">
            <input type="text" disabled="disabled" id="parent_name" name="parent_name" class="form-control"
                   value="<?= $item != null ? $item['parent_name'] : '' ?>">
        </div>
    </div>
    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="name">编码：</label>
        <div class="col-lg-10">
            <input type="text" id="code" name="code" class="form-control"
                   value="<?= isset($item['code']) ? $item['code'] : '' ?>">
        </div>
    </div>
    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="name"><span
                class="font-color-attention">*</span>分类名称：</label>
        <div class="col-lg-10">
            <input type="text" id="name" name="name" class="form-control"
                   value="<?= isset($item['name']) ? $item['name'] : '' ?>">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="sort">排序号：</label>
        <div class="col-lg-10">
            <input type="text" id="sort" name="sort" class="form-control"
                   value="<?= isset($item['sort']) ? $item['sort'] : 1000; ?>">
        </div>
    </div>


    <!--上传图片开始-->
    <iframe id="upload_image_target" name="upload_image_target" style="display: none;"></iframe>
    <form id="small_upload_image_submit_form" enctype="multipart/form-data" method="post"
          action=<?= \app\librarys\Tools::urlBase() . '/main/upload-image-handler' ?> target="upload_image_target">
        <div class="form-group">
            <label class="control-label col-lg-2">上传小图:</label>
            <div class="product-mod col-lg-10">
                <div class="upload-img">
                    <div class="uploadImg">
                        <input class="upload-img-box" type="file" onchange="uploadSmallImage(this)"
                               name="uploadImg" id="uploadImg">
                        <input type="hidden" name="img_cord" id="img_cord"/>
                    </div>
                    <a href="javascript:void(0)" class="btn btn-warning">选择图片(jpeg,png,jpg)</a>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-2"></label>
            <div class="product-mod col-lg-10">

                <div id="imgSingle_Sure">
                    <div class="pre_imgs">
                        <?php if (!empty($item['image_id'])): ?>
                            <img src="<?= \app\models\File::getFile($item['image_id'])['url'] ?>"
                                 style="width: 200px;"
                                 onerror="showErrorImage(this)">
                        <?php endif ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <!--上传图片结束-->


    <div class="form-group">
        <label class="control-label col-lg-2" for="is_show">显示：</label>
        <div class="col-lg-10">
            <div class="checkbox-switch-wrapper">
                <input class="checkbox-switch" type="checkbox" id="is_show" name="is_show"
                    <?php
                    if (isset($item['is_show']) && $item['is_show'] == 1) {
                        echo 'checked="checked"';
                    } else if (!isset($item['is_show'])) {
                        echo 'checked="checked"';
                    }
                    ?>
                >
                <div class="checkbox-switch-circle"></div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2" for="is_enabled">启用：</label>
        <div class="col-lg-10">
            <div class="checkbox-switch-wrapper">
                <input class="checkbox-switch" type="checkbox" id="is_enabled" name="is_enabled"
                    <?php
                    if (isset($item['is_enabled']) && $item['is_enabled'] == 1) {
                        echo 'checked="checked"';
                    } else if (!isset($item['is_enabled'])) {
                        echo 'checked="checked"';
                    }
                    ?>
                >
                <div class="checkbox-switch-circle"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="<?= $web ?>/js/jcrop/jcrop.single.js"></script>
<script type="text/javascript">
    function uploadSmallImage(obj) {
        previewSingleChange(obj, 1, 1, getSmallImgPathFun);
    }

    function getSmallImgPathFun() {
        $imgCord = $("input[name='imgCord']").val();
        $("#small_upload_image_submit_form").find("#img_cord").val($imgCord);

        $("#upload_image_target").html('');
        $("#small_upload_image_submit_form").submit();
        $("#upload_image_target").unbind().load(function () {
            try {
                var result = JSON.parse($("#upload_image_target").contents().find("body").html());
                if (result.status == 0) {
                    var path = '<?= $web?>' + result.data.path;
                    if (result.data.path) {
                        setImgPath(result.data.path);
                    }

                    var html = '<img src="' + path + '" style="width: 200px; margin-left: 10px; margin-top: 10px">';
                    $("#small_upload_image_submit_form").find("#imgSingle_Sure").html('').append(html);
                    closeCutImage();
                } else {
                    errorDialog(result.message);
                }
            } catch (err) {
                errorDialog("保存出错");
            }
        });
    }
</script>